﻿using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Data;
using Red.Controls.ViewModels;
using System.Linq;
using System.Windows;
using System.Windows.Markup;
using System;
using System.Globalization;
using System.Threading.Tasks;
using Red.Core;
using Red.Core.Extensions;

namespace Red.Controls.DataDriven
{
    internal sealed class ActionItemVisibilityConverter : IValueConverter
    {
        #region IValueConverter implementation

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            DataItem di = value as DataItem;
            if ( di != null && di.IsValid )
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException();
        }

        #endregion IValueConverter implementation
    }

    internal sealed class RowActionItemConverter : IValueConverter
    {
        #region IValueConverter implementation
        
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {  
            return new BindableAsyncValue<DataItem>( DigUpActionItem( (DataItem)value, parameter.ToString() ) );
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException();
        }

        private async Task<DataItem> DigUpActionItem( DataItem dc, string param )
        {
            var actionItem = await dc.DigUpSubItemAsync( param );
            return actionItem;
        }

        #endregion IValueConverter implementation
    }

    [InteropControl( "Matrix" )]
    public partial class MatrixControl : UserControl
    {
        public MatrixControl()
        {
            DataContextChanged += MatrixControl_DataContextChanged;
            InitializeComponent();
        }

        private void MatrixControl_DataContextChanged( object sender, System.Windows.DependencyPropertyChangedEventArgs e )
        {
            Rebuild( true );
        }

        private void AddButton_Click( object sender, RoutedEventArgs e )
        {
            DataItem matrixItem = DataContext as DataItem;
            if ( matrixItem != null )
            {
                matrixItem.Execute( "add" );
                matrixItem.RebuildAsync( false ).DoNotAwait();
            }
        }

        private void ActionButton_Click( object sender, RoutedEventArgs e )
        {
            var btn = sender as RedButton;
            var bav = btn.Tag as BindableAsyncValue<DataItem>;
            if ( bav != null )
            {
                bav.Result.Parent.Execute( bav.Result.Title );
            }

            Rebuild();
        }

        private async void Rebuild( bool recursive = false )
        {
            DataItem matrixItem = DataContext as DataItem;
            if ( matrixItem != null )
            {
                await matrixItem.RebuildAsync( recursive );
            }
        }
    }
}
